﻿
// 对layui表格进行全局设置
function layuiTableSet(table, setter) {
    var table_set = {
        headers: { 'Authorization': "Bearer " +setter.getToken().JwtToken },
        request: {
            pageName: 'PageIndex' //页码的参数名称，默认：page
            , limitName: 'PageSize' //每页数据量的参数名，默认：limit
        },
        response: {
            statusName: 'Tag' //规定数据状态的字段名称，默认：code
            , statusCode: 1 //规定成功的状态码，默认：0
            , msgName: 'Message' //规定状态信息的字段名称，默认：msg
            , countName: 'Total' //规定数据总数的字段名称，默认：count
            , dataName: 'Data' //规定数据列表的字段名称，默认：data
        },
        done: function () {
            // 校验权限，如果done被重写了，记得加这个方法
            CheckButtonAuthority();
        }
    };
    table.set(table_set);
}

// 针对表格按钮的权限验证
function CheckButtonAuthority() {

    // 校验按钮权限，没有权限的按钮就隐藏
    if (GetButtonAuthority()) {
        var buttonList = [];
        $('.layui-table-tool-temp').find('a').each(function (i, ele) {
            buttonList.push(ele.id);
        });
        var removeButtonList = GetButtonAuthority(window.location.href, buttonList);
        if (removeButtonList) {
            $.each(removeButtonList, function (i, val) {
                $("#" + val).remove();
            });
        }
    }
}
// 取出需要的权限信息
function GetButtonAuthority(url, buttonList) {
    var noAuthorize = [];
    var dataAuthority = layui.sessionData('dataAuthority').dataAuthority;

    if (dataAuthority) {
        // 超级用户不验证
        if (dataAuthority.IsSystem != 1) {
            var regex = /([a-zA-Z]+)\/(.*)\//;  //match url like http://localhost:5000/OrganizationManage/User/UserIndex

            if (url) {
                url = url.replace(/Manage\//, "/");
                // 根据视图存放位置，切割后面的主要字符串
                var t = url.split(viewPath);
                url = t[t.length - 1];
            }

            var matches = regex.exec(url);
            if (matches && matches.length >= 3) {
                var module = matches[1];
                var page = matches[2];
                buttonList.forEach(function (btn, btnIndex) {
                    var authorize = module.toLowerCase() + ':' + page.toLowerCase() + ':' + btn.toString().replace('btn', '').toLowerCase();
                    var hasAuthority = false;

                    dataAuthority.MenuAuthorize.forEach(function (authority, authorityIndex) {
                        if (authority.Authorize == authorize) {
                            hasAuthority = true;
                            return false;
                        }
                    });

                    if (!hasAuthority) {
                        noAuthorize.push(btn);
                    }
                });
            }
        }
    } else {
        top.window.location.reload();
    }
    return noAuthorize;
}


// 获取get字段
function getRequestData(name) {
    var params = decodeURI(window.location.search);
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
    var r = params.substr(1).match(reg);
    if (r != null) {
        return unescape(r[2]);
    }
    return null;
}

// 初始化layui下拉框 form模块，select id名称，arr数组：[{Key:1,Value:1}]
function setLayuiSelect(form, id, arr, keyName, valueName, Selected) {
    var h = ['<option value=""></option>'];
    for (var i = 0; i < arr.length; i++) {
        var a = arr[i];
        h.push('<option value="');

        if (valueName) {
            h.push(a[valueName]);
            h.push('"');
            if (a[valueName] == Selected) {
                h.push(' Selected ');
            }

        } else {
            h.push(a.Value);
            h.push('"');
            if (a.Value == Selected) {
                h.push(' Selected ');
            }
        }

        h.push('>');

        if (keyName) {
            h.push(a[keyName]);
        } else {
            h.push(a.Key);
        }

        h.push('</option>');
    }

    var str = h.join('');
    if (id.substr(0, 1) == "#") {
        $(id).html(str);
    } else {
        $("#" + id).html(str);
    }

    form.render('select'); //刷新select选择框渲染
}

// 初始化 layui 复选框 form模块，div id名称，name 复选框name， arr数组：[{Key:1,Value:1}]
function setLayuiCheckbox(form, id, name, arr, keyName, valueName) {
    var h = [];
    for (var i = 0; i < arr.length; i++) {
        var a = arr[i];
        h.push('<input type="checkbox" name="');
        h.push(name);
        h.push('" title="');

        if (keyName) {
            h.push(a[keyName]);
        } else {
            h.push(a.Key);
        }

        h.push('" value="');

        if (valueName) {
            h.push(a[valueName]);
        } else {
            h.push(a.Value);
        }

        h.push('"  lay-filter="');
        h.push(name);
        h.push('" lay-skin="primary"> ');
    }

    var str = h.join('');
    if (id.substr(0, 1) == "#") {
        $(id).html(str);
    } else {
        $("#" + id).html(str);
    }

    form.render('checkbox'); //刷新select选择框渲染
}